<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 头像区域 -->
      <div class="avatar-box">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 表单区域 -->
      <div class="login-form">
        <el-form ref="loginFormRef" :model="loginForm" label-width="80px" :rules="loginFormRules">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              v-model="loginForm.password"
              prefix-icon="iconfont icon-3702mima"
              type="password"
            ></el-input>
          </el-form-item>
          <el-form-item label-width="0px" class="btns">
            <el-button type="primary" @click="login">登录</el-button>
            <el-button type="info" @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 表单数据绑定对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 表单验证规则
      loginFormRules: {
        // 用户名验证规则
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度应在3到10个字符之间',
            trigger: 'blur'
          }
        ],
        // 密码验证规则
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 12,
            message: '长度应在6到12个字符之间',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    // 点击重置按钮，重置表单
    resetForm() {
      this.$refs.loginFormRef.resetFields()
    },
    // login () {
    //   this.$refs.loginFormRef.validate( async valid => {
    //     if( !valid ) return
    //     const { data: res} = await this.$http.post('login', this.loginForm)
    //     if( res.meta.status == 200 ) {
    //       console.log('登陆成功')
    //     }else if( res.meta.status == 400 ) {
    //       console.log(res.meta.msg)
    //     }else {
    //       console.log('server error')
    //     }
    //   })
    // }
    login() {
      this.$refs.loginFormRef.validate(valid => {
        if (!valid) return
        this.$http.post('login', this.loginForm).then(
          data => {
            let res = data.data
            if (res.meta.status === 200) {
              this.$message.success('登录成功！')
              // 将登陆成功的状态保持，挂载到session上
              window.sessionStorage.setItem('token', res.data.token)
              this.$router.push('/home')
            } else if (res.meta.status === 400) {
              return this.$message.error(res.meta.msg)
            } else {
              return this.$message.warning('服务器繁忙！')
            }
          },
          () => {
            return this.$message.warning('服务器繁忙！')
          }
        )
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login-container {
  background-color: #2b4b6b;
  height: 100%;
}
.login-box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

  .avatar-box {
    position: absolute;
    top: 0;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 130px;
    height: 130px;
    padding: 10px;
    background-color: #fff;
    border: 1px solid #eee;
    border-radius: 50%;
    box-shadow: 0 0 10px;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #ddd;
    }
  }
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.login-form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding-right: 30px;
  box-sizing: border-box;
}
</style>
